home *** CD-ROM | disk | FTP | other *** search
- /* AuditDocument.c */
- /*
- * DisplayAudit.c
- * Copyright © 1992-93, Apple Computer Inc. All Rights Reserved.
- * Programmed by Martin Minow,
- * Internet: minow@apple.com
- * AppleLink: MINOW
- */
- #include "DisplayAudit.h"
-
- #ifndef TESTING
- #define TESTING 0
- #endif
- #if TESTING
- void TestAuditStatusLocationUsingAnExtremelyLongFunctionName(void);
- pascal void TestPascalVoidFunction(void);
- pascal short TestPascalShortFunction(void);
- pascal OSErr TestPascalFunctionWithParameters(Ptr, Ptr);
- #endif
-
- /*
- * MakeDocumentWindow creates a window that will hold the log information.
- * It then creates/initializes the Audit Record for that window. The strings
- * should be fetched from the resource fork.
- */
- void
- MakeDocumentWindow(void)
- {
- OSErr status;
- register DocumentPtr documentPtr;
- WindowPtr theWindow;
- short windowWidth;
- short windowHeight;
- Point windowTopLeft;
- Rect windowShape;
- SFReply sfReply;
- Str255 windowTitle;
- short listFontNumber;
- StringPtr message;
- Boolean saveAuditEnable;
-
- documentPtr = (DocumentPtr) NewPtrClear(sizeof (DocumentRecord));
- FailNIL(documentPtr, kErrNoMemory);
- TRY {
- AuditFileDialog(
- DLOG_SFPutFile,
- "\pAudit Log File",
- "\pAudit Log",
- &sfReply
- );
- gParameterUpdateNeeded = TRUE;
- DOC.auditIdent = PARM.auditIdent;
- DOC.enableAudit = PARM.enableAudit;
- DOC.scanCount = PARM.auditRecords;
- if (gHasColorQuickDraw) {
- theWindow = GetNewCWindow(
- WIND_Document,
- &DOC.windowRecord,
- (WindowPtr) -1
- );
- }
- else {
- theWindow = GetNewWindow(
- WIND_Document,
- &DOC.windowRecord,
- (WindowPtr) -1
- );
- }
- FailNIL(theWindow, kErrCreateTextWindow);
- if ((long) theWindow != (long) documentPtr) /* Can't happen */
- FailNIL(NULL, kErrCreateTextWindow); /* It did! */
- GetWTitle(WINDOW_PTR, windowTitle);
- BlockMove(
- &DOC.auditIdent,
- &windowTitle[windowTitle[0] + 1 - sizeof (OSType)],
- sizeof (OSType)
- );
- SetWTitle(WINDOW_PTR, windowTitle);
- windowWidth = width(qd.screenBits.bounds) / 2;
- if (windowWidth < kMinWindowWidth)
- windowWidth = kMinWindowWidth;
- windowHeight = height(qd.screenBits.bounds) - (GetMBarHeight() * 2) - 4;
- if (windowHeight < kMinWindowHeight)
- windowHeight = kMinWindowHeight;
- windowTopLeft.h = qd.screenBits.bounds.right - windowWidth;
- windowTopLeft.v = qd.screenBits.bounds.top + GetMBarHeight() * 2 + 2;
- MoveWindow(WINDOW_PTR, windowTopLeft.h, windowTopLeft.v, FALSE);
- SizeWindow(WINDOW_PTR, windowWidth, windowHeight, FALSE);
- BringToFront(WINDOW_PTR);
- ShowWindow(WINDOW_PTR);
- SelectWindow(WINDOW_PTR);
- SetPort(WINDOW_PTR);
- ValidRect(&WINDOW.portRect); /* Eliminate update */
- /*
- * Build a display log in the window.
- * To do: let the user choose the font and size.
- */
- GetFNum(PARM.fontName, &listFontNumber);
- windowShape = WINDOW.portRect;
- windowShape.right -= kScrollBarOffset;
- windowShape.bottom -= kScrollBarOffset;
- DOC.logListHandle = CreateLog(
- &windowShape,
- TRUE, /* Has grow box */
- listFontNumber,
- PARM.fontSize,
- PARM.logDisplayLines
- );
- FailNIL(DOC.logListHandle, kErrCreateTextWindow);
- DrawGrowIcon(WINDOW_PTR);
- /*
- * Create a log file if requested. (Error is non-fatal).
- */
- if (sfReply.good == FALSE)
- DOC.logFileRefNum = 0;
- else {
- status = CreateOutputFile(
- 'ttxt',
- sfReply.fName,
- sfReply.vRefNum,
- &DOC.logFileRefNum
- );
- if (status != noErr) {
- DOC.logFileRefNum = 0;
- ErrorAlert(status, kErrCreateOutputFile, FALSE);
- }
- else {
- DOC.logFileVRefNum = sfReply.vRefNum;
- pstrcpy(DOC.logFileName, sfReply.fName);
- }
- }
- DOC.auditPtr = GetAuditPtr(DOC.auditIdent);
- if (DOC.auditPtr != NULL)
- message = "\pAudit Record was previously created";
- else {
- DOC.auditPtr = InitAudit(
- DOC.auditIdent,
- PARM.auditRecords,
- DOC.enableAudit,
- FALSE
- );
- message = "\pAudit Record created";
- }
- GetCurrentProcess(&DOC.oldPSN);
- WakeUpAudit(DOC.auditPtr, &DOC.oldPSN);
- saveAuditEnable = EnableAudit(DOC.auditPtr, TRUE);
- Audit(
- DOC.auditPtr,
- 'APPL',
- AuditFormat3(
- kAuditFormatHex,
- kAuditFormatAddress,
- kAuditFormatString
- ),
- DOC.auditIdent,
- DOC.auditPtr,
- message
- );
- EnableAudit(DOC.auditPtr, saveAuditEnable);
- #if TESTING
- AuditString(
- DOC.auditPtr,
- 'Moof',
- "\p1234567890123456789012345678901234567890"
- );
- AuditStatusString(DOC.auditPtr, 'Moof', 1, "\pHello world");
- AuditStatusLocation(DOC.auditPtr, 'Moof', 2);
- TestAuditStatusLocationUsingAnExtremelyLongFunctionName();
- TestPascalVoidFunction();
- TestPascalShortFunction();
- TestPascalFunctionWithParameters(NULL, NULL);
- #endif
- }
- CATCH {
- if (DOC.logListHandle != NULL)
- DisposeLog(DOC.logListHandle);
- if (DOC.logFileRefNum != 0) {
- (void) FSClose(DOC.logFileRefNum);
- (void) FSDelete(DOC.logFileName, DOC.logFileVRefNum);
- }
- DisposePtr((Ptr) documentPtr);
- }
- ENDTRY;
- }
-
- void
- ProcessAuditDocument(
- register DocumentPtr documentPtr
- )
- {
- register short i;
-
- for (i = 0; i < DOC.scanCount && ReadAudit(DOC.auditPtr, &ENTRY); i++)
- ProcessAuditEntry(documentPtr);
- }
-
- void
- ProcessAuditEntry(
- register DocumentPtr documentPtr
- )
- {
- Str255 message;
- Str255 work;
- short i;
-
- if (ENTRY.lostData > 0) {
- DOC.totalMissedCount += ENTRY.lostData;
- NumToString(ENTRY.lostData, message);
- pstrcat(message, "\p log entries(");
- NumToString(DOC.totalMissedCount, work);
- pstrcat(message, work);
- pstrcat(message, "\p total) missed...");
- DisplayLogString(DOC.logListHandle, message);
- DOC.logIndex += ENTRY.lostData;
- }
- DOC.logIndex += 1;
- message[0] = 0;
- NumToString(DOC.logIndex, work);
- for (i = work[0]; i < 3; i++)
- pstrcat(message, "\p ");
- pstrcat(message, work);
- pstrcat(message, "\p ");
- FormatAuditEntryTimestamp(DOC.auditPtr, &ENTRY, work);
- /*
- * Since we know that the timestamp has a fixed-length format, we can chop
- * out the date to gain some space in the text window. Note the format:
- * 0 1 2
- * 12345678901234567890123
- * 1993.01.14 22:36:57.123
- */
- work[11] = work[0] - 11;
- pstrcat(message, &work[11]);
- pstrcat(message, "\p ");
- FormatAuditEntryData(&ENTRY, work);
- pstrcat(message, work);
- DisplayLogString(DOC.logListHandle, message);
- if (DOC.logFileRefNum != 0) {
- TRY {
- WriteAuditOutputLine(message, DOC.logFileRefNum);
- }
- CATCH {
- ErrorAlert(STATUS, MESSAGE, FALSE);
- GetIndString(work, STRN_Messages, kErrWriteOutputFile);
- AuditStatusString(DOC.auditPtr, 'APPL', STATUS, work);
- CloseAuditOutputFile(
- STATUS,
- DOC.logFileRefNum,
- DOC.logFileVRefNum,
- DOC.logFileName
- );
- DOC.logFileRefNum = 0;
- NO_PROPAGATE;
- gUpdateMenusNeeded = TRUE;
- }
- ENDTRY;
- }
- }
-
- /*
- * These are passed more-or-less intact to the log manager.
- */
- void
- DoWindowKeyDown(
- register DocumentPtr documentPtr
- )
- {
- #pragma unused (documentPtr)
- /* Nothing happens here */
- }
-
- void
- DoContentClick(
- register DocumentPtr documentPtr
- )
- {
- DoClickInLog(DOC.logListHandle, &EVENT);
- }
-
- void
- UpdateDocumentWindow(
- register DocumentPtr documentPtr
- )
- {
- DrawGrowIcon(WINDOW_PTR);
- UpdateLog(DOC.logListHandle);
- }
-
- void
- ActivateDocumentWindow(
- register DocumentPtr documentPtr,
- Boolean isActivating
- )
- {
- ActivateLog(DOC.logListHandle, isActivating);
- }
-
- void
- DisposeDocumentWindow(
- register DocumentPtr documentPtr
- )
- {
- OSErr status;
- short message;
-
- status = noErr;
- if (DOC.auditPtr != NULL)
- WakeUpAudit(DOC.auditPtr, &DOC.oldPSN);
- if (DOC.logFileRefNum != 0) {
- TRY {
- CloseAuditOutputFile(
- noErr,
- DOC.logFileRefNum,
- DOC.logFileVRefNum,
- DOC.logFileName
- );
- }
- CATCH {
- status = STATUS;
- message = MESSAGE;
- NO_PROPAGATE;
- }
- ENDTRY;
- }
- CloseWindow(WINDOW_PTR);
- DisposePtr((Ptr) documentPtr);
- if (--gOpenWindowCount <= 0)
- gQuitNow = TRUE;
- FailOSErr(status, message);
- }
-
- void
- DecorateWindow(
- register DocumentPtr documentPtr
- )
- {
- SizeLog(
- DOC.logListHandle,
- width(WINDOW.portRect) - kScrollBarOffset,
- height(WINDOW.portRect) - kScrollBarOffset
- );
- }
-
- void
- DoDocumentSaveAs(
- register DocumentPtr documentPtr
- )
- {
- if (DOC.logFileRefNum != 0)
- DoDocumentCloseFile(documentPtr);
- TRY {
- PromptAndCreateAuditOutputFile(
- "\pAudit Output File",
- "\pAudit Log",
- 'ttxt',
- &DOC.logFileRefNum,
- &DOC.logFileVRefNum,
- DOC.logFileName
- );
- }
- CATCH {
- ErrorAlert(STATUS, MESSAGE, FALSE);
- NO_PROPAGATE;
- }
- ENDTRY;
- }
-
- void
- DoDocumentCloseFile(
- register DocumentPtr documentPtr
- )
- {
- TRY {
- CloseAuditOutputFile(
- noErr,
- DOC.logFileRefNum,
- DOC.logFileVRefNum,
- DOC.logFileName
- );
- }
- CATCH {
- ErrorAlert(STATUS, MESSAGE, FALSE);
- NO_PROPAGATE;
- }
- ENDTRY;
- DOC.logFileRefNum = 0;
- gUpdateMenusNeeded = TRUE;
- }
-
- #if TESTING
- /*
- * These were used to test the Audit library and are retained because I'm a kind
- * of sentimental guy.
- */
- #define AuditLocation(ident) ( \
- Audit( \
- ((DocumentPtr) FrontWindow())->auditPtr, \
- (ident), \
- AuditFormat1(kAuditFormatLocation) \
- ) \
- )
-
- void
- TestAuditStatusLocationUsingAnExtremelyLongFunctionName(void) {
- AuditLocation('Moof');
- }
-
- pascal void
- TestPascalVoidFunction(void)
- {
- AuditLocation('Moof');
- }
-
- pascal short
- TestPascalShortFunction(void)
- {
- AuditLocation('Moof');
- return (123);
- }
- pascal OSErr
- TestPascalFunctionWithParameters(
- Ptr fooPtr,
- Ptr barPtr
- )
- {
- AuditLocation('Moof');
- return ((fooPtr == barPtr) ? noErr : paramErr);
- }
- #endif
-
-